package com.example.demo.services;

import com.example.demo.entity.Location.LocationDTO;
import com.example.demo.entity.Product.Files;
import com.example.demo.entity.Product.FilesDTO;
import com.example.demo.entity.Product.Units;
import com.example.demo.entity.Product.WorkProcedure;
import com.example.demo.entity.ProductBillDetail;
import com.example.demo.entity.produceRepairDetailDTO;
import com.example.demo.repository.FilesRepository;
import com.example.demo.repository.ProductBillDetailRepository;
import com.example.demo.repository.UnitCNRepository;
import com.example.demo.repository.WorkProcedureRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Service
public class ProductBillDetailService {
    @Autowired
    private ProductBillDetailRepository repo;
    @Autowired
    private FilesRepository filesRepo;
    @Autowired
    private WorkProcedureRepository workProcedureRepo;
    @Autowired
    private UnitCNRepository unitCNRepo;

    public Page<ProductBillDetail> getProductBillDetail(int page, int size) {

        Pageable pageable = PageRequest.of(page, size);
        Page<Object[]> rawPage = repo.getProductBillDetail(pageable);

        List<ProductBillDetail> details = rawPage.getContent().stream().map(row -> {
            ProductBillDetail pbd = new ProductBillDetail();
            pbd.setPlanCode((String) row[0]);
            pbd.setBillNo((String) row[1]);
            pbd.setChecked((Integer) row[2]);
            pbd.setGoodsCode((String) row[3]);
            pbd.setGoodsName((String) row[4]);
            pbd.setDesignNumberVerSpec((String) row[5]);
            pbd.setUnitCode((String) row[6]);
            pbd.setWorkingAreaCode((String) row[7]);
            pbd.setWorkingCenterCode((String) row[8]);
            pbd.setWpNo((Integer) row[9]);
            pbd.setWpNoLast((Integer) row[10]);
            pbd.setWpCode((String) row[11]);
            pbd.setPlanQty((BigDecimal) row[12]);
            pbd.setId(((Number) row[13]).longValue());
            pbd.setWorkgroupCode((String) row[14]);
            pbd.setWpId(((Number) row[15]).longValue());
            pbd.setInProcessQty((BigDecimal) row[16]);
            pbd.setInQty((BigDecimal) row[17]);
            pbd.setRepairQty((BigDecimal) row[18]);
            pbd.setCompleteQty((BigDecimal) row[19]);
            pbd.setGoodsName2((String) row[20]);
            pbd.setDesignNumberVerSpec2((String) row[21]);
            pbd.setClientName((String) row[22]);
            pbd.setClientGoodsCode((String) row[23]);
            pbd.setOrderNo((String) row[24]);
            pbd.setClientOrderNo((String) row[25]);
            pbd.setNotInQty((BigDecimal) row[26]);
            pbd.setParmMustCompleteWork((Character) row[27]);
            pbd.setCombinationBillNo((String) row[28]);
            pbd.setCombinationWpId(((Number) row[29]).longValue());
            pbd.setRemark((String) row[30]);


            return pbd;
        }).toList();
        System.out.println(details);
        return new PageImpl<>(details, pageable, rawPage.getTotalElements());
    }

    /*获取数据*/
    public List<ProductBillDetail> fetchDetail(String urlData) {
        if (!isPureNumber(urlData)) {
            Long id = repo.getID(urlData);
            return repo.getDetail(id);
        }else {
            String billNo = repo.getBillNo(urlData);
            Long id = repo.getID(billNo);
            return repo.getDetail(id);
        }
    }

    public boolean isPureNumber(String text) {
        if (text == null || text.isEmpty()) {
            return false;
        }
        return text.matches("\\d+");
    }

    public void addDetail(produceRepairDetailDTO payload) {
        String productBillNo = payload.getProductBillNo();
        String wpID = repo.getWpID(productBillNo);
        repo.addRepairDetail(payload.getBillNo(), payload.getProductBillNo(), wpID, payload.getGoodsCode(), payload.getRemark(), payload.getQty());
        return;
    }

    public void updateDetail(produceRepairDetailDTO payload) {
        String productBillNo = payload.getProductBillNo();
        String wpID = repo.getWpID(productBillNo);
        repo.updateRepairDetail(payload.getBillNo(), payload.getProductBillNo(), wpID, payload.getGoodsCode());
        return;
    }

    public String getStatus(String billNo) {
        return repo.getStatus(billNo);
    }

    public boolean getGoodsPower(String goodsCode, String userID) {
        String kindCode = repo.getGoodsKindCode(goodsCode);
        String list = repo.getGoodsKindPower(userID);

        List<String> kindList = Arrays.stream(list.split(","))
                .map(String::trim)
                .toList();
        return kindList.contains("'" + kindCode + "'");
    }

    public FilesDTO getFilesUrl(String goodsCode) {
        FilesDTO filesDTO = new FilesDTO();

        Files files = filesRepo.findImg(goodsCode);
        if (files != null) {
            filesDTO.setImageUrl(files.getFilePathname());
            filesDTO.setPicFormat(files.getPicFormat());
        }
        files = filesRepo.findPDF(goodsCode);
        if (files != null) {
            filesDTO.setPdfUrl(files.getFilePathname());
        }
        return filesDTO;
    }

    public WorkProcedure getWpCn(String wpCode) {
        return workProcedureRepo.getName(wpCode);
    }

    public Units getUnitCn(String unitCode) {
        return  unitCNRepo.getUnitName(unitCode);
    }

    public boolean checkWpLast(String billNo) {
        List<ProductBillDetail> pro = fetchDetail(billNo);
        int wpNo = 0;
        int wpNoLast = 0;
        for (ProductBillDetail pbd : pro) {
            wpNo = pbd.getWpNo();
            wpNoLast = pbd.getWpNoLast();
        }
        if (wpNo != wpNoLast ) {
            return false;
        }else return wpNo != 0;
    }

    public List<LocationDTO> getLocation(String goodsCode) {
        return repo.getLocation(goodsCode);
    }
}

